Skip to content

Verify TLS for MySQL and the remote SQL API, and keep passwords out of logs - #44

Merged
oktie merged 5 commits into
mainfrom
fix/mysql-tls-verification
Nov 20, 2026
Merged

oktie merged 5 commits into
mainfrom
fix/mysql-tls-verification

Conversation

@oktie

@oktie oktie commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Fixes an improper certificate validation report: MySQL connections over TLS had certificate verification turned off, so the toolkit sent MySQL credentials over a connection it never checked. The later commits close the other ways credentials could cross an unchecked connection: a MySQL server could have TLS stripped from its handshake, and RemoteDatabaseConnector sent its API key with verify=False. They also stop the logs from printing database passwords.

TLS verification

Whenever TLS was on, which is always for IBM Cloud Databases hosts, normalize_mysql_connection_string built its context with verify_mode = CERT_NONE and check_hostname = False. Anyone between the toolkit and the server could present any certificate and read the login.

TLS options now move from the connection string into an ssl.SSLContext that always verifies the certificate chain and the host name. There is no option to turn the check off.

Connection string Before After
IBM Cloud host, no TLS options TLS, not verified TLS, verified
sslMode=VERIFY_IDENTITY, any other host no TLS (sslMode was stripped before it was read) TLS, verified
sslMode=REQUIRED or VERIFY_CA not verified on IBM Cloud, no TLS elsewhere TLS, verified (treated as VERIFY_IDENTITY)
ssl=true TLS, not verified TLS, verified
ssl_ca=/path/to/ca.pem handed to aiomysql as a dict, which it can't use (it takes only an SSLContext) TLS, verified against that CA
ssl_check_hostname=false or another unknown ssl* option passed through to the driver refused with a ValueError
no TLS options, sslMode=DISABLED, ssl=false no TLS no TLS

Also in this commit:

  • The per-record database switch keeps the TLS context. It used to rebuild the connection settings from a URL that no longer had the TLS options.
  • A leading sslMode no longer breaks the rest of the query string. The old regex left &charset=... without its ?.
  • scripts/curation/beaver_sql_runner.py and sqlalchemy_schema_extractor.py had their own copies of the old code, and both now use the library's. The extractor used PyMySQL's dict form, which skips the certificate check whenever no CA is named. It also printed the raw connection string.
  • Docs: a "MySQL over TLS" section in docs/guide/configuration.md, and a note in env.example.

Upgrading. A server whose certificate comes from a private CA now fails to connect until that CA is named with ssl_ca=/path/to/ca.pem. IBM Cloud Databases includes its CA with the service credentials. Connect by the host name on the certificate, not by IP address. When the check fails, the connection error says so.

TLS cannot be stripped

aiomysql and PyMySQL treat an SSL context as a preference. When the server's handshake doesn't advertise CLIENT_SSL, they carry on unencrypted and send the login anyway, so an attacker who can rewrite the handshake gets the credentials, even with sslMode=VERIFY_IDENTITY. Copilot flagged this, and the first version of this PR listed it as not covered.

With TLS on, normalize_mysql_connection_string now also returns an async_creator_fn for SQLAlchemy. It builds an aiomysql connection that raises CR_SSL_CONNECTION_ERROR (2026) before authenticating if TLS wasn't offered. The schema extractor does the same for PyMySQL through a do_connect handler.

RemoteDatabaseConnector

Both of its requests sent SQL_API_KEY as a bearer token with verify=False. They now use the default verification from requests, and a private CA is trusted through REQUESTS_CA_BUNDLE. No verify=False, CERT_NONE or disabled host-name check is left in src or scripts.

Passwords in logs

MySQL execution logged its connection string at DEBUG level. That is the toolkit's default level, and a source checkout also writes the log to data/results/bak/log.txt, so every MySQL run put the password on the console and on disk. A new helper, redact_url_password, shows it as ***, and the Beaver curation runner uses it too.

The BIRD Postgres schema converter logged its connection string at info level. It now names only the environment variable it read. It also failed both with and without --pg_conn_str, because an else was indented under the wrong if.

Testing

  • New tests in tests/test_mysql_tls.py, tests/test_connection_string_logging.py and tests/test_remote_connector_tls.py. The full hermetic suite passes (1280 tests), Ruff and Black are clean, and each commit's own tests pass at that commit.
  • The downgrade tests run the real SQLAlchemy and aiomysql path against a fake server on localhost. Against a server that doesn't offer TLS, the connection is refused and the server receives nothing. Without the guard, it receives the 89-byte login packet.
  • End to end, against a fake MySQL server that switches to TLS with a certificate from a throwaway CA, through mysql_run_execution_async and aiomysql:
Case Result
sslMode=VERIFY_IDENTITY, CA not named refused; the login packet never reached the server
sslMode=REQUIRED, CA not named refused
ssl_ca named, host name matches connected; the login was sent only over TLS
ssl_ca named, connecting by IP refused (host name mismatch)
Schema extractor (PyMySQL), ssl_ca named connected; the login was sent only over TLS
Schema extractor, server doesn't offer TLS refused; nothing reached the server

The reported scenario, before and after

An IBM Cloud Databases host (*.databases.appdomain.cloud, resolved to a local fake server) behind an on-path proxy with a self-signed certificate the host doesn't trust, reached through mysql_run_execution_async:

Proxy main This branch
Self-signed certificate, no TLS options in the URL captured the login refused at the TLS handshake; no login sent
Self-signed certificate, sslMode=VERIFY_IDENTITY captured the login refused at the TLS handshake; no login sent
Strips TLS from the MySQL handshake captured the login refused with error 2026; no login sent

Not covered

  • Not tested against a real IBM Cloud Databases instance.
  • The guards against stripped TLS override the private Connection._request_authentication in aiomysql and PyMySQL. If a release renames it, both guards refuse to connect rather than fall back, and the library's downgrade tests run the real driver. The extractor has no automated test, only the manual check above.

Whenever TLS was on for MySQL, which is always for IBM Cloud Databases hosts,
the toolkit set verify_mode to CERT_NONE and check_hostname to False. Anyone
between it and the server could present any certificate and read the
credentials.

TLS options now move from the connection string into an ssl.SSLContext that
always verifies the certificate chain and the host name. sslMode=REQUIRED and
VERIFY_CA are treated as VERIFY_IDENTITY, a private CA is named with ssl_ca,
and unknown ssl* options are refused instead of ignored. A failed certificate
check now says to add ssl_ca.

sslMode was also stripped before it was read, so VERIFY_IDENTITY on any other
host connected without TLS. It now turns TLS on. The per-record database switch
keeps the TLS context instead of rebuilding it from a URL that no longer has
the options.

The Beaver curation runner and the schema extractor had their own copies of the
old code, and both now use the library's.

Signed-off-by: Oktie Hassanzadeh <oktie@oktie.com>
MySQL execution logged its connection string at DEBUG level. That is the
toolkit's default level, and a source checkout also writes the log to
data/results/bak/log.txt, so every MySQL run put the database password on the
console and on disk. A new helper, redact_url_password, shows it as *** in
those messages and in the Beaver curation runner.

The BIRD Postgres schema converter logged its connection string at info level.
It now names only the environment variable it read. It also failed both with
and without --pg_conn_str, because an else was indented under the wrong if.

Signed-off-by: Oktie Hassanzadeh <oktie@oktie.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human security review is needed to assess the retained aiomysql handshake downgrade to plaintext and residual credential-exposure risk, with real IBM Cloud validation unavailable.

No open issues 🎉

What changed in this PR

This PR hardens database connections and logging in the toolkit’s execution library and curation scripts by verifying MySQL certificates when TLS is used and removing passwords from connection-string logs.

Changes:

  • Centralizes MySQL TLS configuration, verifies certificate chains and hostnames, and preserves TLS settings when switching databases.
  • Redacts logged MySQL passwords and fixes PostgreSQL connection-string selection in the BIRD converter.
  • Adds regression tests and documents TLS configuration and private-CA requirements.
File Description
tests/​test_mysql_tls.py Tests TLS options, certificate verification, and context preservation.
tests/​test_connection_string_logging.py Tests password redaction and MySQL logging.
src/​text2sql_eval_toolkit/​execution/​execution_tools.py Centralizes verified TLS handling, preserves contexts, and redacts passwords.
scripts/​curation/​sqlalchemy_schema_extractor.py Uses shared TLS handling and removes insecure fallback and raw URL output.
scripts/​curation/​bird_schema_converter_postgres.py Fixes connection-string selection and removes password-bearing logging.
scripts/​curation/​beaver_sql_runner.py Reuses shared TLS normalization and password redaction.
env.example Adds MySQL TLS and private-CA configuration guidance.
docs/​guide/​configuration.md Documents MySQL TLS options and certificate requirements.
CHANGELOG.md Records TLS verification, logging, and converter fixes.

Review effort: Balanced

aiomysql and PyMySQL treat an SSL context as a preference: when the server's
handshake does not advertise CLIENT_SSL, they carry on unencrypted and send the
login anyway. Anyone who could rewrite that handshake could strip the offer and
collect the credentials, even with sslMode=VERIFY_IDENTITY.

With TLS on, normalize_mysql_connection_string now also returns an
async_creator_fn that builds an aiomysql connection which raises
CR_SSL_CONNECTION_ERROR (2026) before authenticating if TLS was not offered.
The schema extractor does the same for PyMySQL through a do_connect handler.

The new tests run the real SQLAlchemy and aiomysql path against a fake server
on localhost. Without the guard, the server receives the 89-byte login packet,
and with it, nothing.

Signed-off-by: Oktie Hassanzadeh <oktie@oktie.com>
Both of its requests passed verify=False while sending SQL_API_KEY as a bearer
token, so anyone between the toolkit and the API could present any certificate
and read the key. They now use requests' default verification, and a private
CA is trusted through REQUESTS_CA_BUNDLE.

Signed-off-by: Oktie Hassanzadeh <oktie@oktie.com>
The guard against stripped TLS overrides the private
Connection._request_authentication in aiomysql and PyMySQL. If a release
renamed it, the override would silently stop running and the drivers would go
back to logging in without TLS. Both guards now refuse to connect when the
method is missing.

Signed-off-by: Oktie Hassanzadeh <oktie@oktie.com>
@oktie oktie changed the title Verify MySQL TLS certificates and keep passwords out of logs Verify TLS for MySQL and the remote SQL API, and keep passwords out of logs Nov 20, 2026
@oktie
oktie merged commit 85c61d9 into main Nov 20, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants